package com.shigu.kefu.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.opentae.data.mall.beans.ManagerPageConstant;
import com.opentae.data.mall.examples.ManagerPageConstantExample;
import com.opentae.data.mall.interfaces.ManagerPageConstantMapper;
import com.shigu.MallResponse;
import com.shigu.constant.enums.KefuConstant;
import com.shigu.constant.enums.PageUri;
import com.shigu.constant.enums.VipSetting;
import com.shigu.constant.service.SystemConstantService;
import com.shigu.kefu.bo.AskListBo;
import com.shigu.kefu.vo.AdminConcactResponse;
import com.shigu.kefu.vo.AskListVo;
import com.shigu.kefu.vo.ConcactInfoResponse;
import com.shigu.kefu.vo.QuickUpgradeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/*
 * create by  过儿  2019-05-06
 * 客服信息服务
 */
@Service
public class ContactService {
    @Autowired
    SystemConstantService systemConstantService;
    @Autowired
    ManagerPageConstantMapper managerPageConstantMapper;

    /**
     * @description 手机端查客服信息
     * @author 郭尔
     * @date 2019-05-06 10:54
     */
    public ConcactInfoResponse getContactInfo(){
        // 所有系统常量
        Map<String, String> map = systemConstantService.selPageConstant( PageUri.system.toString());
        String workTime = map.get(KefuConstant.worktime.toString());
        String imQQ = map.get(KefuConstant.imQQ.toString());
        String imWx = map.get(KefuConstant.imWx.toString());
        String asks = map.get(KefuConstant.askList.toString());

        List<AskListVo> data = JSONObject.parseArray(asks, AskListVo.class);
        ConcactInfoResponse response = new ConcactInfoResponse();
        response.setAskList(data);
        response.setWorkTime(workTime);
        response.setImQQ(imQQ);
        response.setImWx(imWx);

        return response.success();

    }

    /**
     *
     * @description 客服信息表
     * @author 郭尔
     * @date 2019-05-06 11:04
     */
     public MallResponse modifyContact(String filedName,String value){
          ManagerPageConstantExample example = new ManagerPageConstantExample();
          example.createCriteria().andFieldNameEqualTo(filedName);
          ManagerPageConstant constant = new ManagerPageConstant();
          constant.setFieldValue(value);

          managerPageConstantMapper.updateByExampleSelective(constant,example);
          //修改后让原来的map重新加载一遍,否则后台数据无法刷新
          systemConstantService.loadConstantValue();
          return new MallResponse().success();
      }

    /**
     *
     * @description 修改问答列表
     * @author 郭尔
     * @date 2019-05-06 12:41
     */
     public MallResponse modifyAskList(List<AskListBo> data){
         ManagerPageConstantExample example = new ManagerPageConstantExample();
         example.createCriteria().andFieldNameEqualTo(KefuConstant.askList.toString());

         ManagerPageConstant constant = new ManagerPageConstant();
         constant.setFieldValue(JSON.toJSONString(data));

         managerPageConstantMapper.updateByExampleSelective(constant,example);
         //修改后让原来的map重新加载一遍
         systemConstantService.loadConstantValue();
         return new MallResponse().success();
     }

    /**
     * @description 快速升级
     * @author 郭尔
     * @date 2019-11-12 11:14
     */
    public QuickUpgradeResponse quickUpgrade(){
         Map<String, String> map = systemConstantService.selPageConstant( PageUri.system.toString());
         String imgSrc = map.get(KefuConstant.wxImgSrc.toString());
         String contactText = map.get(KefuConstant.upgradeDescription.toString());
        return new QuickUpgradeResponse(imgSrc,contactText);
     }

    /**
     * @description 手机端查客服信息
     * @author 郭尔
     * @date 2019-05-06 10:54
     */
    public AdminConcactResponse kefuContact(){
        // 所有系统常量
        Map<String, String> map = systemConstantService.selPageConstant( PageUri.system.toString());

        Map<String, String> vipMap = systemConstantService.selPageConstant(PageUri.vip.toString());

        String workTime = map.get(KefuConstant.worktime.toString());
        String imQQ = map.get(KefuConstant.imQQ.toString());
        String imWx = map.get(KefuConstant.imWx.toString());
        String asks = map.get(KefuConstant.askList.toString());
        String imgSrc = map.get(KefuConstant.wxImgSrc.toString());
        String contactText = map.get(KefuConstant.upgradeDescription.toString());
        String buttonImage = vipMap.get(VipSetting.vipPriceButtonImage.toString());


        List<AskListVo> data = JSONObject.parseArray(asks, AskListVo.class);
        AdminConcactResponse response = new AdminConcactResponse();
        response.setAskList(data);
        response.setWorkTime(workTime);
        response.setImQQ(imQQ);
        response.setImWx(imWx);
        response.setContactText(contactText);
        response.setWxImgSrc(imgSrc);
        response.setVipPriceButtonImage(buttonImage);

        return response.success();

    }
}
